﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace PblReader
{
    public static class PblReport
    {
        public static void SrdXrefReport(string pblPath, TextWriter writer)
        {
            foreach (var pblFileName in Directory.GetFiles(pblPath, "*.pbl"))
            {
                var pbl = new PblReader(pblFileName);
                var pblName = Path.GetFileNameWithoutExtension(pblFileName);

                foreach (PblEntry entry in pbl.Entries.Values.Where(_ => _.ObjectName.EndsWith(".srd")))
                {
                    try
                    {
                        var printed = false;
                        var srd = SrdParser.Parse(pbl.GetContentAsString(entry));
                        var srdName = Path.GetFileNameWithoutExtension(entry.ObjectName);

                        foreach (var reference in srd.SelectNodes("/srd/report/dataobject").Cast<XmlNode>().Select(_ => _.InnerText))
                        {
                            writer.WriteLine(pblName + "\t" + srdName + "\t" + reference);
                            printed = true;
                        }

                        if (!printed)
                        {
                            writer.WriteLine(pblName + "\t" + srdName + "\t");
                        }
                    }
                    catch { }
                }
            }
        }

        public static void DirectoryReport(string pblPath, TextWriter writer)
        {
            foreach (var pblFileName in Directory.GetFiles(pblPath, "*.pbl"))
            {
                var pbl = new PblReader(pblFileName);

                foreach (var entry in pbl.Entries.Values.OrderBy(_=>_.ObjectName))
                {
                    var comment = pbl.GetComment(entry);

                    Console.WriteLine("{0}\t{1}\t{2}", pbl.Name, entry.ObjectName, comment);
                }
            }
        }

        public static void ExtractPbl(string _pblFileName, string _extractFolder)
        {
            var pbl = new PblReader(_pblFileName);
            var folder = Path.Combine(_extractFolder, pbl.Name);

            if (Directory.Exists(folder))
            {
                Directory.Delete(folder, true);
            }

            Directory.CreateDirectory(folder);

            foreach (var entry in pbl.Entries.Values)
            {
                if (entry.IsSource)
                {
                    using (var writer = new StreamWriter(Path.Combine(folder, entry.ObjectName), false))
                    {
                        writer.Write(pbl.GetContentAsString(entry));
                    }
                }

                Console.WriteLine("{0}\t{1}", pbl.Name, entry.ObjectName);
            }
        }

        public static void ExtractAllPbl(string _pblFolder, string _extractFolder)
        {
            foreach (var pblFileName in Directory.GetFiles(_pblFolder, "*.pbl"))
            {
                ExtractPbl(pblFileName, _extractFolder);
            }
        }
    }
}
